﻿using System;
using System.Collections.Generic;
using System.Text;
using ArkEC.CoreCom.CacheStore;
using System.Data;
using ArkEC.SEMs.DataAccess.MLM;
using System.Data.Common;
using ArkEC.SEMs.DataAccess;
using ArkEC.CoreCom.Datastruct;
using ArkEC.CoreCom.Runtime;

namespace ArkEC.SEMs.Business.MLM
{
    /// <summary>
    /// 语言资源缓存
    /// </summary>
    public class LanResCache : CacheType
    {
        /// <summary>
        /// 
        /// </summary>
        private static string _CacheNameField = "MultiLanguage";

        #region 构造函数

        /// <summary>
        /// 唯一实例
        /// </summary>
        private static LanResCache mInstance = new LanResCache();
        public static LanResCache Instance
        {
            get 
            {
                return mInstance; 
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        private LanResCache() 
        {
            this.CacheName = _CacheNameField;
        }

        #endregion

        /// <summary>
        /// 读取缓存数据
        /// </summary>
        /// <returns></returns>
        public override Dictionary<string, object> ReadCache()
        {
            Dictionary<string, object> languageRes = new Dictionary<string, object>();

            DataTable resTable = null;
            using (DbConnection conn = ConnFactory.CreateConn())
            {
                resTable = MLMDAFactory.CreateLanResDA().QuerySysValueRes(conn, null);
            }

            //循环语言资源行
            foreach (DataRow resRow in resTable.Rows)
            {
                string keyCode = ObjectConvertion.DbToString(resRow["KeyCode"]);

                Dictionary<string, string> resItemList = null;
                //资源关键字存在
                if (languageRes.ContainsKey(keyCode))
                {
                    resItemList = languageRes[keyCode] as Dictionary<string, string>;
                }
                else
                {
                    resItemList = new Dictionary<string, string>();
                    languageRes.Add(keyCode, resItemList);
                }

                resItemList.Add(ObjectConvertion.DbToString(resRow["LRCode"]), ObjectConvertion.DbToString(resRow["LanValue"]));
            }

            return languageRes;
        }

        /// <summary>
        /// 通过KeyCode获取语言资源
        /// </summary>
        /// <param name="keyCode"></param>
        /// <returns></returns>
        public static string GetLanValue(string keyCode)
        {
            string keyValue = "";

            object lanValue = CacheHelper.GetItem(LanResCache.Instance, keyCode);
            if (lanValue != null)
            {
                Dictionary<string, string> resItemList = lanValue as Dictionary<string, string>;
                string currLan = RunSetting.GetRunningLanguage();

                if (resItemList.ContainsKey(currLan))
                {
                    keyValue = resItemList[currLan];
                }
            }

            return keyValue;
        }
    }
}
